using CollabApp.Domain.Entities.Game;

namespace CollabApp.Domain.Services.Game;

/// <summary>
/// 游戏结果服务接口
/// 负责计算游戏结果、排名、奖励分配和成就解锁
/// </summary>
public interface IGameResultService
{
    /// <summary>
    /// 计算游戏最终结果
    /// 基于游戏类型和规则，计算所有玩家的最终成绩和排名
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>游戏结果详情</returns>
    Task<GameResult> CalculateGameResultAsync(Guid gameId);

    /// <summary>
    /// 计算玩家排名
    /// 根据得分、完成时间等因素确定玩家排名
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>玩家排名列表</returns>
    Task<List<PlayerRanking>> CalculatePlayerRankingsAsync(Guid gameId);

    /// <summary>
    /// 计算经验值奖励
    /// 基于游戏表现和排名计算玩家获得的经验值
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>经验值奖励详情</returns>
    Task<ExperienceReward> CalculateExperienceRewardAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 计算积分奖励
    /// 基于游戏结果计算玩家获得的积分
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>积分奖励详情</returns>
    Task<ScoreReward> CalculateScoreRewardAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 检查成就解锁
    /// 检查玩家在游戏中是否达成了特定成就
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>解锁的成就列表</returns>
    Task<List<Achievement>> CheckAchievementUnlocksAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 生成游戏统计报告
    /// 创建详细的游戏数据统计报告
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>统计报告</returns>
    Task<GameStatisticsReport> GenerateStatisticsReportAsync(Guid gameId);

    /// <summary>
    /// 计算玩家评级变化
    /// 基于游戏结果更新玩家的技能评级
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>评级变化详情</returns>
    Task<RatingChange> CalculateRatingChangeAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 保存游戏结果到历史记录
    /// 将游戏结果持久化到数据库
    /// </summary>
    /// <param name="gameResult">游戏结果</param>
    /// <returns>保存是否成功</returns>
    Task<bool> SaveGameResultAsync(GameResult gameResult);

    /// <summary>
    /// 获取历史游戏结果
    /// 检索指定游戏的历史结果数据
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>历史游戏结果，如果不存在则返回null</returns>
    Task<GameResult?> GetGameResultAsync(Guid gameId);

    /// <summary>
    /// 计算团队奖励
    /// 为团队游戏计算额外的团队协作奖励
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="teamId">团队标识</param>
    /// <returns>团队奖励详情</returns>
    Task<TeamReward> CalculateTeamRewardAsync(Guid gameId, Guid teamId);
}

/// <summary>
/// 游戏结果
/// </summary>
public class GameResult
{
    public Guid GameId { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public TimeSpan Duration { get; set; }
    public GameType GameType { get; set; }
    public GameEndReason EndReason { get; set; }
    public List<PlayerResult> PlayerResults { get; set; } = new();
    public GameStatistics Statistics { get; set; } = new();
    public Dictionary<string, object> Metadata { get; set; } = new();
}

/// <summary>
/// 玩家结果
/// </summary>
public class PlayerResult
{
    public Guid PlayerId { get; set; }
    public string PlayerName { get; set; } = string.Empty;
    public int FinalScore { get; set; }
    public int Rank { get; set; }
    public TimeSpan PlayTime { get; set; }
    public bool IsWinner { get; set; }
    public PlayerStatistics Statistics { get; set; } = new();
    public List<Achievement> AchievementsUnlocked { get; set; } = new();
    public ExperienceReward ExperienceGained { get; set; } = new();
    public ScoreReward ScoreGained { get; set; } = new();
    public RatingChange RatingChange { get; set; } = new();
}

/// <summary>
/// 玩家排名
/// </summary>
public class PlayerRanking
{
    public Guid PlayerId { get; set; }
    public string PlayerName { get; set; } = string.Empty;
    public int Rank { get; set; }
    public int Score { get; set; }
    public float TerritoryPercentage { get; set; }
    public int KillCount { get; set; }
    public int DeathCount { get; set; }
    public TimeSpan SurvivalTime { get; set; }
    public Dictionary<string, object> CustomMetrics { get; set; } = new();
}

/// <summary>
/// 经验值奖励
/// </summary>
public class ExperienceReward
{
    public int BaseExperience { get; set; }
    public int BonusExperience { get; set; }
    public int TotalExperience { get; set; }
    public List<ExperienceSource> Sources { get; set; } = new();
    public int LevelBefore { get; set; }
    public int LevelAfter { get; set; }
    public bool LeveledUp { get; set; }
}

/// <summary>
/// 积分奖励
/// </summary>
public class ScoreReward
{
    public int BaseScore { get; set; }
    public int BonusScore { get; set; }
    public int TotalScore { get; set; }
    public List<ScoreSource> Sources { get; set; } = new();
    public float Multiplier { get; set; } = 1.0f;
}

/// <summary>
/// 成就
/// </summary>
public class Achievement
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public AchievementType Type { get; set; }
    public int Points { get; set; }
    public DateTime UnlockedAt { get; set; }
    public Dictionary<string, object> Criteria { get; set; } = new();
}

/// <summary>
/// 游戏统计报告
/// </summary>
public class GameStatisticsReport
{
    public Guid GameId { get; set; }
    public DateTime GeneratedAt { get; set; }
    public TimeSpan GameDuration { get; set; }
    public int TotalPlayers { get; set; }
    public int TotalActions { get; set; }
    public Dictionary<string, int> ActionBreakdown { get; set; } = new();
    public PlayerStatistics TopPerformer { get; set; } = new();
    public List<GameEvent> KeyEvents { get; set; } = new();
    public Dictionary<string, object> CustomMetrics { get; set; } = new();
}

/// <summary>
/// 玩家统计
/// </summary>
public class PlayerStatistics
{
    public Guid PlayerId { get; set; }
    public int ActionsPerformed { get; set; }
    public int KillCount { get; set; }
    public int DeathCount { get; set; }
    public float TerritoryControlled { get; set; }
    public float MaxTerritoryControlled { get; set; }
    public int ItemsCollected { get; set; }
    public int SkillsUsed { get; set; }
    public float DistanceTraveled { get; set; }
    public TimeSpan TimeAlive { get; set; }
    public Dictionary<string, int> DetailedStats { get; set; } = new();
}

/// <summary>
/// 评级变化
/// </summary>
public class RatingChange
{
    public int RatingBefore { get; set; }
    public int RatingAfter { get; set; }
    public int Change { get; set; }
    public RatingTier TierBefore { get; set; }
    public RatingTier TierAfter { get; set; }
    public bool TierChanged { get; set; }
    public string Reason { get; set; } = string.Empty;
}

/// <summary>
/// 团队奖励
/// </summary>
public class TeamReward
{
    public Guid TeamId { get; set; }
    public int BaseReward { get; set; }
    public int CooperationBonus { get; set; }
    public int TotalReward { get; set; }
    public List<TeamMemberReward> MemberRewards { get; set; } = new();
}

/// <summary>
/// 团队成员奖励
/// </summary>
public class TeamMemberReward
{
    public Guid PlayerId { get; set; }
    public int IndividualReward { get; set; }
    public int TeamBonus { get; set; }
    public int TotalReward { get; set; }
}

/// <summary>
/// 经验值来源
/// </summary>
public class ExperienceSource
{
    public string Source { get; set; } = string.Empty;
    public int Amount { get; set; }
    public string Description { get; set; } = string.Empty;
}

/// <summary>
/// 积分来源
/// </summary>
public class ScoreSource
{
    public string Source { get; set; } = string.Empty;
    public int Amount { get; set; }
    public string Description { get; set; } = string.Empty;
}

/// <summary>
/// 成就类型
/// </summary>
public enum AchievementType
{
    Combat,         // 战斗相关
    Territory,      // 领土相关
    Survival,       // 生存相关
    Collection,     // 收集相关
    Social,         // 社交相关
    Special         // 特殊成就
}

/// <summary>
/// 评级等级
/// </summary>
public enum RatingTier
{
    Bronze,
    Silver,
    Gold,
    Platinum,
    Diamond,
    Master,
    Grandmaster
}
